/**
 * Copyright 2011 Sibaozuan(superskysbz@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package summary.java.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/**
 * @author Sibaozuan(superskysbz@gmail.com)
 */
public class JavaHttpSummary {
	private String path;
	private String targetFile;
	private int threadNum;
	private int fileSize;
	private DownloadThread[] threads;

	public JavaHttpSummary(String path, String targetFile, int threadNum) {
		this.path = path;
		this.targetFile = targetFile;
		this.threadNum = threadNum;
		threads = new DownloadThread[threadNum];
	}

	public void download() throws Exception {
		URL url = new URL(path);
		HttpURLConnection mConnection = (HttpURLConnection) url.openConnection();
		mConnection.setConnectTimeout(5 * 1000);
		mConnection.setRequestMethod("GET");
		mConnection.setRequestProperty("Accept", "image/gif,image/jpge,*/*");
		mConnection.setRequestProperty("Accept-Language", "zh-CN");
		mConnection.setRequestProperty("Charset", "UTF-8");
		mConnection.setRequestProperty("User-Agent", "");
		mConnection.setRequestProperty("Connection", "Keep-Alive");
		fileSize = mConnection.getContentLength();
		mConnection.disconnect();
		int currentPartSize = fileSize / threadNum + 1;
		RandomAccessFile rAccessFile = new RandomAccessFile(targetFile, "rw");
		rAccessFile.setLength(fileSize);
		rAccessFile.close();
		for (int i = 0; i < threadNum; i++) {
			int startPosition = i * currentPartSize;
			RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
			currentPart.seek(startPosition);
			threads[i] = new DownloadThread(startPosition, currentPartSize, currentPart);
		}
	}

	class DownloadThread extends Thread {
		private int startPosition;
		private int currentPartSize;
		private RandomAccessFile currentPart;
		private int length;

		public DownloadThread(int startPosition, int currentPartSize,
				RandomAccessFile currentPart) {
			this.startPosition = startPosition;
			this.currentPartSize = currentPartSize;
			this.currentPart = currentPart;
		}

		@Override
		public void run() {
			try {
				URL url = new URL(path);
				HttpURLConnection mConnection = (HttpURLConnection) url.openConnection();
				mConnection.setConnectTimeout(5 * 1000);
				mConnection.setRequestMethod("GET");
				mConnection.setRequestProperty("Accept", "image/gif,image/jpge,*/*");
				mConnection.setRequestProperty("Accept-Language", "zh-CN");
				mConnection.setRequestProperty("Charset", "UTF-8");
				InputStream mInputStream = mConnection.getInputStream();
				mInputStream.skip(startPosition);
				byte[] buffer = new byte[1024];
				int hasRead = 0;
				while (length < currentPartSize
						&& (hasRead = mInputStream.read(buffer)) != -1) {
					currentPart.write(buffer, 0, hasRead);
					length += hasRead;
				}
				currentPart.close();
				mInputStream.close();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (ProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
